//队列的链表实现：头部出队，尾部入队，比较容易处理，即单链表指针向右移动比较容易，向左移动比较困难gg
#include "Queue1.h"
#include <stdio.h>
#include <stdlib.h>

struct _node {
    ElementType data;
    struct _node* next;
};

struct _queue {
    Node* head;
    Node* tail;
};

Queue* CreateQueue() {
    Queue* q = (Queue*)malloc(sizeof(struct _queue));
    q->head = NULL;
    q->tail = NULL;
    return q;
}

void DisposeQueue(Queue* q) {
    if (q) {
        if (!IsEmpty(q)) {
            DeQueue(q);
        }
    }
}

//在头部出队，尾部入队
void EnQueue(Queue* q, ElementType ele) {
    if (q->head == NULL) {
        q->head = (Node*)malloc(sizeof(struct _node));
        q->head->next = NULL;
        q->head->data = ele;
        q->tail = q->head;
    } else {
        Node* newNode = (Node*)malloc(sizeof(struct _node));
        newNode->data = ele;
        newNode->next = NULL;
        q->tail->next = newNode;
        q->tail = newNode;
    }
}

ElementType DeQueue(Queue* q) {
    if (q) {
        if (q->head) {
            Node* oldNode = q->head;
            ElementType res = oldNode->data;
            q->head = oldNode->next;
            free(oldNode);
            return res;
        }
    }
    return -1;
}

int IsEmpty(Queue* q) {
    if (q->head)
        return 0;
    return 1;
}

void TestQueue() {
    Queue* q = CreateQueue();
    for (int i = 0; i < 10; i++) {
        EnQueue(q, i);
    }
    while (!IsEmpty(q)) {
        printf("%d\n", DeQueue(q));
    }

    DisposeQueue(q);
}
